
/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

#include "stdafx.h"
#include "FCTabPage.h"

namespace FaceCat{
    FCTabPage::FCTabPage(){
        m_headerButton = 0;
        m_headerLocation.x = 0;
        m_headerLocation.y = 0;
        m_tabView = 0;
    }
    
    FCTabPage::~FCTabPage(){
        if (m_headerButton)
        {
            m_headerButton->removeEvent((FCEventCallBack*)this, L"ondragbegin");
            m_headerButton->removeEvent((FCEventCallBack*)this, L"ondragend");
            m_headerButton->removeEvent((FCEventCallBack*)this, L"ondragging");
            m_headerButton->removeEvent((FCTouchEventCallBack*)this, L"ontouchdown");
        }
        m_headerButton = 0;
        m_tabView = 0;
    }
    
    FCButton* FCTabPage::getHeaderButton(){
        return m_headerButton;
    }
    
    void FCTabPage::setHeaderButton(FCButton *value){
        m_headerButton = value;
    }
    
    FCPoint FCTabPage::getHeaderLocation(){
        return m_headerLocation;
    }
    
    void FCTabPage::setHeaderLocation(FCPoint value){
        m_headerLocation = value;
    }
    
    bool FCTabPage::isHeaderVisible(){
        if(m_headerButton){
            return m_headerButton->isVisible();
        }
        else{
            return false;
        }
    }
    
    void FCTabPage::setHeaderVisible(bool value){
        if(m_headerButton){
            m_headerButton->setVisible(value);
        }
    }
    
    FCTabView* FCTabPage::getTabView(){
        return m_tabView;
    }
    
    void FCTabPage::setTabView(FCTabView *value){
        m_tabView = value;
    }
    
    ////////////////////////////////////////////////////////////////////////////////////////////

    void FCTabPage::callEvent(String eventName, Object sender, Object invoke){
        FCDiv::callEvent(eventName, sender, invoke);
        if(eventName == L"ondragbegin"){
            getTabView()->onDragTabHeaderBegin(this);
        }else if(eventName == L"ondragend"){
            getTabView()->onDragTabHeaderEnd(this);
        }else if(eventName == L"ondragging"){
            getTabView()->onDraggingTabHeader(this);
        }
    }

    void FCTabPage::callTouchEvent(String eventName, Object sender, FCTouchInfo touchInfo, Object invoke){
        FCDiv::callTouchEvent(eventName, sender, touchInfo, invoke);
        if (sender == m_headerButton) {
            if (eventName == L"ontouchdown") {
                if (m_tabView) {
                    m_tabView->setSelectedTabPage(this);
                    m_tabView->update();
                    m_tabView->invalidate();
                }
            }
        }
    }
    
    String FCTabPage::getViewType(){
        return L"TabPage";
    }
    
    void FCTabPage::getAttribute(const String& name, String *value, String *type){
        if(name == L"headersize"){
            *type = L"size";
            if(m_headerButton){
                *value = FCTran::sizeToStr(m_headerButton->getSize());
            }
            else{
                *value = L"0,0";
            }
        }
        else if(name == L"headervisible"){
            *type = L"bool";
            *value = FCTran::boolToStr(isHeaderVisible());
        }
        else if ((int)name.find(L"header-") != -1){
            if (m_headerButton){
                m_headerButton->getAttribute(name.substr(7), value, type);
            }
        }
        else{
            FCDiv::getAttribute(name, value, type);
        }
    }
    
    ArrayList<String> FCTabPage::getAttributeNames(){
        ArrayList<String> attributeNames = FCDiv::getAttributeNames();
        attributeNames.add(L"Header");
        attributeNames.add(L"HeaderSize");
        attributeNames.add(L"HeaderVisible");
        return attributeNames;
    }
    
    void FCTabPage::onLoad(){
        FCDiv::onLoad();
        if(m_tabView){
            if (!m_headerButton){
                FCHost *host = getNative()->getHost();
                m_headerButton = dynamic_cast<FCButton*>(host->createInternalView(this, L"headerbutton"));
                m_headerButton->addEvent((FCEventCallBack*)this, L"ondragbegin", this);
                m_headerButton->addEvent((FCEventCallBack*)this, L"ondragend", this);
                m_headerButton->addEvent((FCEventCallBack*)this, L"ondragging", this);
                m_headerButton->addEvent((FCTouchEventCallBack*)this, L"ontouchdown", this);
            }
            if(!m_tabView->containsView(m_headerButton)){
                m_tabView->addView(m_headerButton);
            }
        }
    }
    
    void FCTabPage::onTextChanged(){
        FCDiv::onTextChanged();
        if(m_headerButton){
            m_headerButton->setText(getText());
        }
    }
    
    void FCTabPage::setAttribute(const String& name, const String& value){
        if(name == L"headersize"){
            if(m_headerButton){
                m_headerButton->setAttribute(L"size", value);
            }
        }
        else if(name == L"headervisible"){
            setHeaderVisible(FCTran::strToBool(value));
        }
        else if ((int)name.find(L"header-") != -1){
            if (m_headerButton){
                m_headerButton->setAttribute(name.substr(7), value);
            }
        }
        else{
            FCDiv::setAttribute(name, value);
        }
    }
}
